package com.xkcoding.dubbo.consumer.controller;


import com.xkcoding.dubbo.provider.service.HelloService;
import com.xkcoding.dubbo.provider.service.User;
import com.xkcoding.dubbo.provider.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * Hello服务API
 * </p>
 *
 * @package: com.xkcoding.dubbo.consumer.controller
 * @description: Hello服务API
 * @author: yangkai.shen
 * @date: Created in 2018-12-25 17:22
 * @copyright: Copyright (c) 2018
 * @version: V1.0
 * @modified: yangkai.shen
 */
@RestController
@Slf4j
public class HelloController {
    @DubboReference(timeout = 5000,check = false,retries = 0)
    private HelloService helloService;
    @DubboReference(check = false)
    private UserService userService;

    @GetMapping("/sayHello")
    public String sayHello(@RequestParam(defaultValue = "xkcoding") String name) {
        log.info("i'm ready to call someone......");
        long currentTimeMillis = System.currentTimeMillis();
        try {
            helloService.sayHello(name);
            RpcContext.getServerContext().setAttachment("dkb","dengkaibing");
            userService.addUser(new User());
            log.info("rpcContext:{}",RpcContext.getServerContext());

        }catch (Exception e) {
            log.error("rpc invoke error",e);
        }
        long end = System.currentTimeMillis();
        return System.currentTimeMillis()+"-"+currentTimeMillis+"耗时："+(end-currentTimeMillis);

    }


}
